﻿<%@ WebHandler Language="C#" Class="Active" %>

using System;
using System.Web;
using System.Collections.Generic;

public class Active : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    string msg = string.Empty;
    
    Gportal.GCMS.Entity.Content_Type_TypeTree TypeTreeEntity;
    Gportal.GCMS.Data.Content_Type_TypeTree TypeTreeHelper = new Gportal.GCMS.Data.Content_Type_TypeTree();
    Gportal.GCMS.Data.Content_Entity ContentHelper = new Gportal.GCMS.Data.Content_Entity();
    
    int ACTIVE_ID = 164;
    public void ProcessRequest(HttpContext context)
    {
        CurrentContext = context;
        try
        {
            string action = context.Request.QueryString["action"].ToString();
            switch (action.ToLower())
            {
                case "list":
                    GetActiveList();
                    break;

                case "detail":
                    GetDetail();
                    break;
            }
        }
        catch (Exception err)
        {
            msg = "{\"result\":\"error\",\"msg\":\"" + err.Message + "\"}";
        }
        finally
        {
            context.Response.Write(msg);
        }
    }
    public HttpContext CurrentContext;

    public void GetDetail()
    {
        string ContentId = GetQuery("id").ToString();
        
        string errmsg = string.Empty;
        int pageindex = 1;
        int pagesize = 10;
        if (string.IsNullOrEmpty(ContentId))
        {
            msg = "{\"result\":\"error\",\"msg\":\"缺少参数Id\"}";
            return; 
        }
        if (!int.TryParse(GetQuery("page", "1"), out pageindex))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数 当前页数 填写不正确。\"}";
            return;
        }
        if (!int.TryParse(GetQuery("size", "10"), out pagesize))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数 每页数量 填写不正确。\"}";
            return;
        }
        int count = 0;

        System.Collections.Generic.List<Gportal.GCMS.Entity.Content_Entity> list = new List<Gportal.GCMS.Entity.Content_Entity>();


        //string sql = " Content_Entity.Status=4 and TypeTreeId=164 and " + strWhere;
        //List<Gportal.GCMS.Entity.Content_Entity> list = new List<Gportal.GCMS.Entity.Content_Entity>();
        //TypeTreeEntity = TypeTreeHelper.GetEntityByTreeId(ACTIVE_ID);
        //list = ContentHelper.GetListArrayUnion(sql, TypeTreeEntity, "", PageSize, PageIndex, ref count);
        Gportal.GCMS.Entity.Content_Entity entity = ContentHelper.GetEntityWithRelationEntity(Convert.ToInt32(ContentId));
        //Gportal.GCMS.Entity.Content_Entity entity = ContentHelper.GetEntityByContentId(Convert.ToInt32(ContentId));
        //list = GetList(" ContentId="+ContentId+" ", pageindex, pagesize, ref count);
        if (string.IsNullOrEmpty(errmsg))
        {
            msg = "{\"result\":\"success\",\"count\":1,\"data\":[";
           

                msg += "{\"title\":\"" + entity.GetFieldValue("Title") + "\",";
                msg += "\"id\":\"" + entity.GetFieldValue("ContentId") + "\",";
                msg += "\"pic\":\"http://203.90.227.74/" + entity.GetFieldValue("Pic") + "\",";
                msg += "\"piccontent\":\"" + JSON.CoatnvertJson(entity.GetFieldValue("Picture_Notes")) + "\",";
                msg += "\"content\":\"" + JSON.CoatnvertJson(entity.GetFieldValue("Description")) + "\",";
                msg += "\"url\":\"/guideapp/active.html?id=" + entity.ContentId.ToString() + "\"}";//**
               
            
            msg += "]}";
        }
        else
            msg = "{\"result\":\"error\",\"msg\":\"" + errmsg + "\"}";

    }
    
    public void GetActiveList()
    {
        string errmsg = string.Empty;
        int pageindex = 1;
        int pagesize=10;
        if (!int.TryParse(GetQuery("page", "1"), out pageindex))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数 当前页数 填写不正确。\"}";
            return ;
        }
        if (!int.TryParse(GetQuery("size", "10"), out pagesize))
        {
            msg = "{\"result\":\"error\",\"msg\":\"参数 每页数量 填写不正确。\"}";
            return;
        }   
        int count=0;

        System.Collections.Generic.List<Gportal.GCMS.Entity.Content_Entity> list = new List<Gportal.GCMS.Entity.Content_Entity>();
        
        list=GetList(" 1=1 ",pageindex,pagesize,ref count);
        if (string.IsNullOrEmpty(errmsg))
        {
            msg = "{\"result\":\"success\",\"count\":" + count.ToString() + ",\"data\":[";
            for (int i = 0; i < list.Count; i++)
            {
                Gportal.GCMS.Entity.Content_Entity entity = list[i];

                msg += "{\"title\":\"" + entity.GetFieldValue("Title") + "\",";
                msg += "\"id\":\"" + entity.GetFieldValue("ContentId") + "\",";
                msg += "\"pic\":\"http://203.90.227.74/" + entity.GetFieldValue("Pic") + "\",";
                msg += "\"content\":\"" + JSON.CoatnvertJson(entity.GetFieldValue("Picture_Notes")) + "\",";
                msg += "\"url\":\"http://203.90.227.74/guideapp/active.html?id=" + entity.ContentId.ToString() + "\"}";//**
                //msg += "\"url\":\"/guideapp/active.html?id=" + entity.ContentId.ToString() + ".shtml\"}";//**
                if (i < list.Count - 1)
                    msg += ",";
            }
            msg += "]}";
        }
        else
            msg = "{\"result\":\"error\",\"msg\":\"" + errmsg + "\"}";

    }

    public System.Collections.Generic.List<Gportal.GCMS.Entity.Content_Entity> GetList(string strWhere, int PageIndex, int PageSize, ref int count)
    {
        string sql = " Content_Entity.Status=4 and TypeTreeId=164 and " + strWhere;
        List<Gportal.GCMS.Entity.Content_Entity> list = new List<Gportal.GCMS.Entity.Content_Entity>();
        TypeTreeEntity = TypeTreeHelper.GetEntityByTreeId(ACTIVE_ID);
        list = ContentHelper.GetListArrayUnion(sql, TypeTreeEntity, "", PageSize, PageIndex, ref count);
        return list;
    }

    public string GetQuery(string name)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        return res;
    }
    public string GetQuery(string name, string defaultvalue)
    {
        string res = string.Empty;
        if (CurrentContext.Request.QueryString[name] != null)
            res = CurrentContext.Request.QueryString[name].ToString();
        if (string.IsNullOrEmpty(res)) res = defaultvalue;
        return res;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}